package com.kinghunt.misp.web.controller.configx;

import com.kinghunt.misp.common.dto.configx.OrgConfigDTO;
import com.kinghunt.misp.common.dto.configx.SysConfigDTO;
import com.kinghunt.misp.common.dto.userx.UserDTO;
import com.kinghunt.misp.service.api.configx.ConfigService;
import com.bygoodtech.kings.common.dto.ApiResult;
import com.bygoodtech.kings.common.utils.Checker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

import static com.kinghunt.misp.web.context.SystemContext.getCurrentUser;

/**
 * 配置管理
 *
 * @Author jiangtao
 * @Date 2023/7/22 13:01
 */
@RestController
@RequestMapping("/api/configx/config/")
public class ConfigController {
    @Autowired(required = false)
    private ConfigService configService;

    /**
     * 获取系统级配置
     *
     * @return
     */
    @GetMapping("getSysConfig")
    public ApiResult<SysConfigDTO> getSysConfig() {
        SysConfigDTO result = Checker.success(configService.getSysConfig());
        return ApiResult.ok(result);
    }

    /**
     * 获取机构级配置
     *
     * @param orgId
     * @return
     */
    @GetMapping("getOrgConfig")
    public ApiResult<OrgConfigDTO> getOrgConfig(Long orgId) {
        OrgConfigDTO result = Checker.success(configService.getOrgConfig(orgId));
        return ApiResult.ok(result);
    }

    /**
     * 更新系统级配置
     *
     * @param sysConfigDTO
     * @return
     */
    @PostMapping(value = "updateSysConfig", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ApiResult<Boolean> updateSysConfig(@RequestBody SysConfigDTO sysConfigDTO) {
        Checker.success(configService.updateSysConfig(sysConfigDTO));
        return ApiResult.ok(Boolean.TRUE);
    }

    /**
     * 更新机构级配置
     * @param orgConfigDTO
     * @return
     */
    @PostMapping(value = "updateOrgConfig", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResult<Boolean> updateOrgConfig(@RequestBody OrgConfigDTO orgConfigDTO) {
        Checker.success(configService.updateOrgConfig(orgConfigDTO.getOrgId(), orgConfigDTO));
        return ApiResult.ok(Boolean.TRUE);
    }

    /**
     * 上传查重文件
     *
     * @param file
     * @param orgId
     * @param request
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/uploadDuplicateCheckFile")
    @ResponseBody
    public ApiResult<String> uploadDuplicateCheckFile(@RequestParam("file") MultipartFile file,
                                            @RequestParam("orgId") Long orgId,
                                            HttpServletRequest request)
            throws IOException {
        UserDTO loginUser = getCurrentUser();
        String uploadResult = Checker.success(configService.uploadDuplicateCheckFile(loginUser, file, orgId, request));
        return ApiResult.ok(uploadResult);
    }
}
